package com.magist.manage.web.controller;

import com.github.pagehelper.PageInfo;
import com.magist.core.controller.BaseController;
import com.magist.core.entity.ResultMessage;
import com.magist.core.exception.BusinessException;
import com.magist.manage.web.dto.SysRoleDto;
import com.magist.manage.web.entity.SysRole;
import com.magist.manage.web.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(value="权限接口",tags={"权限接口"})
@RestController
@RequestMapping("/rest/sysRole")
public class RestSysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation(value = "保存", notes = "保存接口")
    @RequestMapping(value = "save.do", method = RequestMethod.POST)
    public Object save(@ModelAttribute("sysRoleDto") SysRoleDto sysRoleDto) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDto, sysRole);
        sysRole.setId(sysRoleService.getNewId());
        sysRoleService.insert(sysRole);
        return ResultMessage.success();
    }

    @ApiOperation(value = "更新", notes = "更新接口")
    @RequestMapping(value = "update.do", method = RequestMethod.POST)
    public Object update(@ModelAttribute("sysRoleDto") SysRoleDto sysRoleDto) {
        SysRole oldSysRole = sysRoleService.getById(sysRoleDto.getId());
        if (oldSysRole == null)
            throw new BusinessException("无效的数据!");
        ResultMessage result = null;
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDto, sysRole);
        sysRole.setModifyTime(oldSysRole.getModifyTime());
        if (sysRoleService.update(sysRole) > 0)
            result = ResultMessage.success("操作成功");
        else
            result = ResultMessage.error("操作失败!");
        return result;
    }

    @ApiOperation(value = "列表", notes = "分页查询接口")
    @RequestMapping(value = "page.do", method = RequestMethod.GET)
    public Object page(@ModelAttribute("sysRoleDto") SysRoleDto sysRoleDto) {
        ResultMessage result = ResultMessage.success();
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDto, sysRole);
        if (!StringUtils.isEmpty(sysRole.getRoleName())) {
            sysRole.setRoleName("%" + sysRole.getRoleName() + "%");
        }
        if (!StringUtils.isEmpty(sysRole.getRemark())) {
            sysRole.setRemark("%" + sysRole.getRemark() + "%");
        }

        PageInfo<SysRole> pageInfo = sysRoleService.page(sysRole, getPageInfo());
        result.setContent(pageInfo);
        return result;
    }

    @ApiOperation(value = "列表", notes = "列表查询接口")
    @RequestMapping(value = "list.do", method = RequestMethod.GET)
    public Object list(@ModelAttribute("sysRoleDto") SysRoleDto sysRoleDto) {
        ResultMessage result = ResultMessage.success();
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDto, sysRole);
        List<SysRole> list = sysRoleService.find(sysRole);
        result.setContent(list);
        return result;
    }

    @ApiOperation(value = "删除", notes = "删除接口")
    @RequestMapping(value = "del.do", method = RequestMethod.POST)
    public Object del(@ModelAttribute("id") String id) {
        SysRole sysRole = sysRoleService.getById(id);
        if (sysRole == null)
            throw new BusinessException("无效的数据!");
        ResultMessage result = null;
        if (sysRoleService.delete(sysRole) > 0)
            result = ResultMessage.success("操作成功");
        else
            result = ResultMessage.error("操作失败!");
        return result;
    }

    @ApiOperation(value = "详情", notes = "详情接口")
    @RequestMapping(value = "info.do", method = RequestMethod.GET)
    public Object info(@ModelAttribute("id") String id) {
        SysRole sysRole = sysRoleService.getById(id);
        if (sysRole == null)
            throw new BusinessException("无效的数据!");
        SysRoleDto sysRoleDto = new SysRoleDto();
        BeanUtils.copyProperties(sysRole, sysRoleDto);
        ResultMessage result = ResultMessage.success();
        result.setContent(sysRoleDto);
        return result;
    }
}
